#if UNITY_ANDROID || UNITY_IOS
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using FibMatrix.IO;
using FM_Mono;

[IgnoreGen]
public abstract class MobileScriptConfig:IScriptConfig
{
    protected enum MobilePersistType
    {
        Package,
        Disk
    }
    
#if (UNITY_IPHONE || UNITY_TVOS || UNITY_WEBGL || UNITY_SWITCH) && !UNITY_EDITOR
    const string DLLNAME = "__Internal";
#else
    const string DLLNAME = "native_helper";
#endif

    [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
    protected static extern void AddAssemblySearchPath(string prefix, MobilePersistType type);

    [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
    protected static extern void ResetAssemblySearchPath();
    
    [MonoPInvokeCallback(typeof(FM_MonoEnv.ImageLoadCallback))]
    [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
    protected static extern IntPtr LoadImage(string aname, string culture, out uint outSize);

    [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
    protected static extern void SetAssemblyOnlyReadFromPackage(string aname);

    protected MobileScriptConfig(List<SearchPath> searchPaths, string[] packageOnlyAssemblies)
    {
        ResetAssemblySearchPath();
        foreach (var searchPath in searchPaths)
        {
            AddAssemblySearchPath(searchPath.GetRootFullPath(), (MobilePersistType)(int)searchPath.persistType);
        }
        // AddAssemblySearchPath(Path.Combine(StorageManager.persistentDataPath, ResourcePathConst.REMOTE_ASSET_DIRECTORY, ResourcePathConst.ASSEMBLIES_DIRECTORY_PREFIX), MobilePersistType.Disk);
        // AddAssemblySearchPath(Path.Combine(PackageManager.streamingAssetsPath, ResourcePathConst.ASSEMBLIES_DIRECTORY_PREFIX), MobilePersistType.Package);

        if (packageOnlyAssemblies != null)
        {
            foreach (var aname in packageOnlyAssemblies)
            {
                SetAssemblyOnlyReadFromPackage(aname);
            }
        }
    }

    public abstract string libraryDirectory { get; }
    public abstract string configDirectory { get; }
    public abstract string overrideConfig { get; }

    public virtual FM_MonoEnv.ImageLoadCallback imageLoadFunc
    {
        get
        {
            return LoadImage;
        }
    }
}
#endif
